import _ from 'lodash';
/* eslint no-useless-escape:0 import/prefer-default-export:0 */

export const findById = (list, id) => {
  let result = null;
  list.forEach(value => {
    if (value.id === id) {
      result = value;
    }
  });
  return result;
};
export const buildTree = (list, pid = '0') => {
  const tree = [];
  list.forEach(value => {
    const x = value;
    if (`${x.pid}` === pid) {
      if (hasChildren(list, x)) {
        delete x.isLeaf;
        x.children = [];
        x.children = buildTree(list, x.id);
      } else {
        delete x.children;
        x.isLeaf = true;
      }
      tree.push(x);
    }
  });
  return tree;
};
export const hasChildren = (list, item) => {
  let result = false;
  list.forEach(value => {
    if (value.pid === item.id) {
      result = true;
    }
  });
  return result;
};

export const getExpandedRowKeys = list => {
  const expandedRowKeys = [];
  _.forEach(list, value => {
    expandedRowKeys.push(value.id);
  });
  return expandedRowKeys;
};
